package com.zliblike_platform.controller;

import com.zliblike_platform.dto.UserUpdateDTO;
import com.zliblike_platform.entity.User;
import com.zliblike_platform.service.UserService;
import com.zliblike_platform.vo.ResponseResult;
import com.zliblike_platform.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    
    /**
     * 用户中心页面
     */
    @GetMapping("/user/profile")
    public String userProfile(HttpSession session, Model model) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return "redirect:/login";
        }
        
        UserVO userVO = userService.getUserById(loginUser.getId());
        model.addAttribute("user", userVO);
        
        return "user/profile";
    }
    
    /**
     * 修改密码页面
     */
    @GetMapping("/user/change-password")
    public String changePasswordPage(HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return "redirect:/login";
        }
        
        return "user/change-password";
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/api/user/info")
    @ResponseBody
    public ResponseResult<UserVO> getUserInfo(HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "未登录");
        }
        
        UserVO userVO = userService.getUserById(loginUser.getId());
        return ResponseResult.success(userVO);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/api/user/info")
    @ResponseBody
    public ResponseResult<Void> updateUserInfo(@RequestBody UserUpdateDTO updateDTO, HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "未登录");
        }
        
        try {
            boolean success = userService.updateUser(loginUser.getId(), updateDTO);
            if (success) {
                // 更新session中的用户信息
                session.setAttribute("loginUser", userService.findByUsernameOrEmail(updateDTO.getUsername()));
                return ResponseResult.success();
            } else {
                return ResponseResult.fail(400, "更新失败");
            }
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 修改密码
     */
    @PostMapping("/api/user/change-password")
    @ResponseBody
    public ResponseResult<Void> changePassword(
            @RequestParam String oldPassword,
            @RequestParam String newPassword,
            HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return ResponseResult.fail(401, "未登录");
        }
        
        try {
            boolean success = userService.updatePassword(loginUser.getId(), oldPassword, newPassword);
            if (success) {
                return ResponseResult.success();
            } else {
                return ResponseResult.fail(400, "修改失败");
            }
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
    
    /**
     * 查看下载历史页面
     */
    @GetMapping("/user/downloads")
    public String downloadsPage(HttpSession session) {
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return "redirect:/login";
        }
        
        return "user/downloads";
    }
} 